﻿    using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.InsuranceWarrantyDealWith;
using ZBJF.Warranty.Model.ViewModel.ServiceProvider;
using ZBJF.Warranty.WebProvider;

namespace ZBJF.Warranty.AdminSite.Controllers
{
    /// <summary>
    /// 工单处理字段管理
    /// </summary>
    public class InsuranceWarrantyDealWithController : BaseController
    {

        #region 工单状态管理

        #region 工单状态管理页面 + ActionResult InsuranceWarrantyRecordStatusIndex()
        /// <summary>
        /// 工单状态管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult InsuranceWarrantyRecordStatusIndex()
        {
            var list = ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance.LoadAllEntity().ToList();
            //ViewBag.model = list;
            return View(list);
        }
        public ActionResult ShowStatusList(List<InsuranceWarrantyRecordStatus> listPage, int pid = 0, int layer = 0)
        {
            ViewBag.Pid = pid;
            ViewBag.Layer = layer;
            return View(listPage);
        }

        #endregion

        #region 保存工单状态 + ActionResult SaveInsuranceWarrantyRecordStatus(InsuranceWarrantyRecordStatus model)

        [HttpPost]
        [ValidateInput(false)]
        //保存工单状态
        public ActionResult SaveInsuranceWarrantyRecordStatus(InsuranceWarrantyRecordStatus model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };

            var serv = ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance;

            if (model.ID == 0)
            {
                if (serv.IsExist(model.StatusName, model.StatusCode))
                {
                    responseResult.Message = "该状态名称或者状态编码已存在";
                }
                else
                {
                    model.CreateTime = DateTime.Now;
                    model.CreateBy = CurrentUser.UserAccount;
                    model.LastModifyBy = CurrentUser.UserAccount;
                    model.LastModifyTime = DateTime.Now;
                    var entity = serv.AddEntities(model);
                    responseResult.Success = entity.ID > 0;
                }
            }
            else
            {
                var entity = serv.LoadByID(model.ID);
                if (entity != null)
                {
                    if (entity.StatusName != model.StatusName && serv.IsExist(model.StatusName, model.ID))
                    {
                        responseResult.Message = "已存在同名的工单状态，请修改工单状态名称";
                    }
                    else
                    {
                        entity = ReflectHelper<InsuranceWarrantyRecordStatus, InsuranceWarrantyRecordStatus>.CloneT1ToT2(model, entity);
                        entity.LastModifyBy = CurrentUser.UserAccount;
                        entity.LastModifyTime = DateTime.Now;
                        responseResult.Success = ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance.UpdateEntities(entity);
                    }
                }
                else
                {
                    responseResult.Message = "该工单状态不存在或已被删除";
                }
            }
            TempData["ResponseResult"] = responseResult;
            return RedirectToAction("InsuranceWarrantyRecordStatusIndex");
        }


        #endregion

        #region 禁用/启用工单状态 + JsonResult InsuranceWarrantyRecordStatusUpdateStatus(int id, bool isAvailable)
        /// <summary>
        /// 禁用/启用工单状态
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="isAvailable">启用/禁用</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult InsuranceWarrantyRecordStatusUpdateStatus(int id, bool isAvailable)
        {
            var entity = ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance.LoadByID(id);
            var success = false;
            if (entity != null)
            {

                entity.IsAvailable = isAvailable;
                entity.LastModifyBy = CurrentUser.UserAccount;
                entity.LastModifyTime = DateTime.Now;
                success = ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance.UpdateEntities(entity);
            }
            return Json(new
            {
                success = success,
                message = success ? "修改成功" : "修改失败"
            });
        }
        #endregion

        #region 根据父级工单状态ID获取子级工单状态列表
        public JsonResult GetChildrenStatus(int parentId)
        {
            var cityList = ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance.GetChildrenStatus(null , parentId).Select(o => new
            {
                o.ID,
                o.StatusName
            });
            //var selectList = new SelectList(cityList, "CityID", "CityName");
            return Json(cityList, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #endregion

        #region 状态原因管理

        #region 状态原因列表 +  ActionResult StatusReasonIndex()
        /// <summary>
        /// 状态原因列表
        /// </summary>
        /// <param name="id">所属工单状态ID</param>
        /// <returns></returns>
        public ActionResult StatusReasonIndex()
        {
            return View();
        }
        #endregion

        #region 状态原因管理页面获取列表数据 + ActionResult StatusReasonList(StatusReasonModel model)
        /// <summary>
        /// 状态原因管理页面获取列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult StatusReasonList(StatusReasonModel model)
        {

            var total = 0;
            var list = ServiceFactory.StatusReasonServiceInstance.GetPagingList(model, out total).AsNoTracking().ToList().Select(o => new
            {
                o.ID,
                o.ReasonDescribe,
                StatusName = o.InsuranceWarrantyRecordStatus.StatusName,
                o.InsuranceWarrantyRecordStatusId,
                ParentStatusId= o.InsuranceWarrantyRecordStatus.ParentId,
                o.IsAvailable,
                LastModifyTime = o.LastModifyTime.ToString("yyyy-MM-dd HH:mm:ss"),
                o.LastModifyBy,
            });
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 状态原因编辑 + ActionResult StatusReasonEdit(int id)
        /// <summary>
        /// 状态原因编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult StatusReasonEdit(int id)
        {
            var accessoriesType = ServiceFactory.StatusReasonServiceInstance.LoadByID(id);
            return View(accessoriesType);
        }

        #endregion

        #region 保存状态备注 + ActionResult SaveStatusReason(StatusReason model)

        [HttpPost]
        [ValidateInput(false)]
        //保存状态备注
        public ActionResult SaveStatusReason(StatusReason model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };

            var serv = ServiceFactory.StatusReasonServiceInstance;

            if (model.ID == 0)
            {
                if (serv.IsExist(model.ReasonDescribe))
                {
                    responseResult.Message = "该状态备注已经存在";
                }
                else
                {
                    model.CreateTime = DateTime.Now;
                    model.CreateBy = CurrentUser.UserAccount;
                    model.LastModifyBy = CurrentUser.UserAccount;
                    model.LastModifyTime = DateTime.Now;
                    var entity = serv.AddEntities(model);
                    responseResult.Success = entity.ID > 0;
                }
            }
            else
            {
                var entity = serv.LoadByID(model.ID);
                if (entity != null)
                {
                    if (entity.ReasonDescribe != model.ReasonDescribe && serv.IsExist(model.ReasonDescribe, model.ID))
                    {
                        responseResult.Message = "已存在同名的状态备注，请修改状态备注";
                    }
                    else
                    {
                        entity = ReflectHelper<StatusReason, StatusReason>.CloneT1ToT2(model, entity);
                        entity.LastModifyBy = CurrentUser.UserAccount;
                        entity.LastModifyTime = DateTime.Now;
                        responseResult.Success = ServiceFactory.StatusReasonServiceInstance.UpdateEntities(entity);
                    }
                }
                else
                {
                    responseResult.Message = "该状态名称不存在或已被删除";
                }
            }
            TempData["ResponseResult"] = responseResult;
            return RedirectToAction("StatusReasonIndex");
        }
        #endregion

        #region 禁用/启用状态名称 + JsonResult StatusReasonUpdateStatus(int id, bool isAvailable)
        /// <summary>
        /// 禁用/启用状态名称
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="isAvailable">启用/禁用</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult StatusReasonUpdateStatus(int id, bool isAvailable)
        {
            var entity = ServiceFactory.StatusReasonServiceInstance.LoadByID(id);
            var success = false;
            if (entity != null)
            {

                entity.IsAvailable = isAvailable;
                entity.LastModifyBy = CurrentUser.UserAccount;
                entity.LastModifyTime = DateTime.Now;
                success = ServiceFactory.StatusReasonServiceInstance.UpdateEntities(entity);
            }
            return Json(new
            {
                success = success,
                message = success ? "修改成功" : "修改失败"
            });
        }
        #endregion

        #endregion

    }
}